/*
 * main.cpp
 *
 *  Created on: Mar 4, 2011
 *      Author: stud
 */

#include <stdlib.h>
#include <cstdio>
#include <iostream>
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <pthread.h>
#include "MessageHandling.h"

key_t key;

struct POINT3D {
    int x,y,z;
};

void* receiver(void* foo)
{
	key = msgget(100, 0666 | IPC_CREAT);
	POINT3D buf3D;
    while (1)
    {
    	printf("WTF");
        if (MessageHandling::receiveMsg<POINT3D>(key, buf3D) >= 0)
        	printf("Received %i, %i, %i\n", buf3D.x, buf3D.y, buf3D.z);
        else
        	printf("fail");
    }
    return 0;
}

void* sender(void* foo)
{
	key = msgget(100, 0666 | IPC_CREAT);
    POINT3D point3D = { 0,0,0 };
    while (point3D.x < 10)
    {
        MessageHandling::sendMsg<POINT3D>(key, point3D);
        point3D.x++;
        point3D.y++;
        point3D.z++;
        sleep(1);
    }
    return 0;
}

int main()
{
    //key = msgget(100, 0666 | IPC_CREAT);
    pthread_t send, recv;
    pthread_create(&send, NULL, sender, NULL);
    pthread_create(&recv, NULL, receiver, NULL);
    void* ret;
    pthread_join(send, &ret);
    msgctl(key, IPC_RMID, NULL); // clean up
    return 0;
}
